﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Meraz.Core.Model;
using System.Text;

namespace Meraz.Web.Views.Helpers
{
    public static class BlogStudyHelper
    {
        static string deleteFormat = @"<button class='delete' type='button' value='{0}' onclick='window.PageBus.publish({1}, this);'>Delete</button>";
        public static string PosterFor(this HtmlHelper view, Poster poster)
        {
            return CreatePosterOption(poster);
        }
        public static string LinkFor(this HtmlHelper html, Link link)
        {
            return CreateLinkOption(link);
        }
        public static string IssueFor(this HtmlHelper html, Issue issue)
        {
            return CreateIssueOption(issue);
        }

        public static string LoadPostFor(this HtmlHelper html, Post post)
        {
            string permalink = (post != null) ? post.Permalink : string.Empty;
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<div class='semantic'>");
            sb.AppendLine("<label for='Post_Permalink'>Permalink</label>");
            sb.AppendLine("<div class='extend'>");
            sb.AppendFormat("<input type='text' class='medium' id='Post_Permalink' name='Post.Permalink' value='{0}' />", permalink);
            sb.AppendLine();
            sb.AppendLine("<button type='submit' class='download'>Load</button>");
            if (!string.IsNullOrWhiteSpace(permalink))
                sb.AppendFormat("<a href='{0}' class='extlink' target='_blank'>New Window</a>", post.Permalink);
            sb.AppendLine("</div>");
            sb.AppendLine("</div>");

            return sb.ToString();
        }
        public static string PermalinkFor(this HtmlHelper html, Post post)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine();
            sb.AppendLine("<label for='Post_Permalink'>Permalink</label>");
            sb.AppendLine("<div class='extend'>");
            sb.AppendFormat("<input disabled='true' type='text' class='medium' id='Post_Permalink' name='Post.Permalink' value='{0}' />", post.Permalink);
            sb.AppendLine();
            sb.AppendFormat("<a href='{0}' class='extlink' target='_blank'>New Window</a>", post.Permalink);
            sb.AppendLine("</div>");
            return sb.ToString();
        }

        public static string CreateLinkOption(Link link)
        {
            string[] values = new string[] { "Positive", "Negative", "Neutral" };
            string name = "link-" + link.ID;
            StringBuilder sb = new StringBuilder();
            sb.AppendLine();
            sb.AppendFormat(deleteFormat, link.ID, "EditPostHandler.removeLink");
            sb.AppendLine();
            sb.AppendLine("<div class='radiosAsButtons'>");
            for (int valueIndex = 0; valueIndex < values.Length; valueIndex++)
            {
                sb.AppendFormat("<input type='radio' id='{0}-{1}' name='{0}' value='{2}' ", name, valueIndex, values[valueIndex]);
                if (link.Tone.Equals(values[valueIndex]))
                    sb.Append("checked='true'");
                sb.AppendLine("/>");
                sb.AppendFormat("<label for='{0}-{1}'>{2}</label>", name, valueIndex, values[valueIndex]);
                sb.AppendLine();
            }
            sb.AppendLine("</div>");
            sb.AppendFormat("<a href='{0}'><span id='{1}' class='link'>{0}</span></a>", link.Url.Name, name);
            sb.AppendLine();
            return sb.ToString();
        }

        public static string CreateIssueOption(Issue issue)
        {
            string[] values = new string[] { "Positive", "Negative", "Neutral" };
            string name = "issue-" + issue.ID;
            StringBuilder sb = new StringBuilder();
            sb.AppendLine();
            sb.AppendFormat(deleteFormat, issue.ID, "EditPostHandler.removeIssue");
            sb.AppendLine();
            sb.AppendLine("<div class='radiosAsButtons'>");
            for (int valueIndex = 0; valueIndex < values.Length; valueIndex++)
            {
                sb.AppendFormat("<input type='radio' id='{0}-{1}' name='{0}' value='{2}' ", name, valueIndex, values[valueIndex]);
                if (issue.AffectiveFrame.Equals(values[valueIndex]))
                    sb.Append("checked='true'");
                sb.AppendLine("/>");
                sb.AppendFormat("<label for='{0}-{1}'>{2}</label>", name, valueIndex, values[valueIndex]);
                sb.AppendLine();
            }
            sb.AppendLine("</div>");
            sb.AppendFormat("<span id='{0}' class='issue'>{1}</span>",name, issue.Topic.Name);
            return sb.ToString();
        }

        public static string CreatePosterOption(Poster poster)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine();
            sb.AppendFormat(deleteFormat, poster.ID, "EditPostHandler.removePoster");
            sb.AppendLine();
            sb.AppendFormat("<span id='poster-{0}' class='poster'>{1}</span>", poster.ID, poster.Alias.Name);
            sb.AppendLine();
            return sb.ToString();
        }
    }
}